package test;

import static org.junit.Assert.*;

import java.util.ArrayList;

import movement.Location;

import org.junit.Before;
import org.junit.Test;

import unit.FireTruck;
import unit.PoliceCar;
import unit.Unit;

import dispatch.DispatchCenter;
import emergency.*;
import emergency.Fire.Size;

public class testEmergency {

	private EmergencyBoard emergencyBoard;
	private Emergency fire, robbery, trafficaccident, publicdisturbance;
	@SuppressWarnings("unused")
	private Emergency nullSeverity, nullDispatchCenter, nullLocation;
	private DispatchCenter center;
	Unit u1,u2;
	
	@Test
	public void cancelEmergency(){
		assertEquals(fire.getStateName(),"staterecorded");
		assertTrue(center.getEmergencyboard().getEmergencies().contains(fire));
		fire.cancelEmergency();
		assertEquals(fire.getStateName(),"statecanceled");
		assertTrue(center.getEmergencyboard().getEmergencies().contains(fire));
		assertTrue(center.getEmergencyboard().getEmergenciesCancelled().contains(fire));
	}

	@Test
	public void completeEmergency(){
		assertEquals(fire.getStateName(),"staterecorded");
		assertTrue(center.getEmergencyboard().getEmergencies().contains(fire));
		fire.sendUnits();
		fire.complete();
		assertEquals(fire.getStateName(),"statecompleted");
		assertTrue(center.getEmergencyboard().getEmergencies().contains(fire));
		assertTrue(center.getEmergencyboard().getEmergenciesCompleted().contains(fire));
	}

	@Test (expected=IllegalStateException.class)
	public void completeInvalidEmergency(){
		assertEquals(fire.getStateName(),"staterecorded");
		assertTrue(center.getEmergencyboard().getEmergencies().contains(fire));
		fire.complete();
		assertEquals(fire.getStateName(),"statecompleted");
		assertTrue(center.getEmergencyboard().getEmergencies().contains(fire));
		assertTrue(center.getEmergencyboard().getEmergenciesCompleted().contains(fire));
	}

	@Test
	public void createEmergencyAndAddLegalEmergencies() {
		assertTrue(emergencyBoard.getEmergencies().size() == 0);
		emergencyBoard.addEmergency(fire);
		assertTrue(emergencyBoard.getEmergencies().contains(fire));
		emergencyBoard.addEmergency(robbery);
		assertTrue(emergencyBoard.getEmergencies().contains(robbery));
		emergencyBoard.addEmergency(trafficaccident);
		assertTrue(emergencyBoard.getEmergencies().contains(trafficaccident));
		emergencyBoard.addEmergency(publicdisturbance);
		assertTrue(emergencyBoard.getEmergencies().contains(publicdisturbance));
	}

	@Test(expected = IllegalArgumentException.class)
	public void createIllegalEmergency() {
		nullSeverity = new Fire(new Location(10, 10, "Foch"), null, center,
				Size.LOCAL, true, 5, 5);
		nullDispatchCenter = new Fire(new Location(10, 10, "Foch"),
				Severity.BENIGN, null, Size.LOCAL, true, 5, 5);
		nullLocation = new Fire(null, Severity.BENIGN, center, Size.LOCAL,
				true, 5, 5);
	}
	
	@Before
	public void setUp() throws Exception {
		center = new DispatchCenter("DC 1", new Location(45, 45,
				"DC 1 - Location"), new Location(60, 60, "GarageLocation"));
		emergencyBoard = new EmergencyBoard(center);
		fire = new Fire(new Location(10, 10, "Foch"), Severity.BENIGN, center,
				Size.LOCAL, true, 5, 5);
		robbery = new Robbery(new Location(20, 20, "Brussel"), Severity.URGENT,
				center, true, true);
		trafficaccident = new TrafficAccident(new Location(15, 15, "Leuven"),
				Severity.NORMAL, center, 3, 1);
		publicdisturbance = new PublicDisturbance(new Location(15, 18,
				"Naamsestraat"), Severity.SERIOUS, center, 3);
		
		u1 = new FireTruck("FT", 120, center.getGarage(), Fire.Size.FACILITY);
		u2 = new PoliceCar("PC", 160, center.getGarage());
	}
	
	@Test
	public void testAddEmergencyToDC(){
		Emergency em = new Robbery(new Location(10,10,"jaja"), Severity.NORMAL, center, false, false);
		// add em to dc is called
		assertTrue(center.getEmergencyboard().getEmergencies().contains(em));
	}
	
	@Test
	public void testGetSuggestion(){
		Robbery robb = new Robbery(new Location(20, 20, "Brussel"), Severity.URGENT,
				center, false, false);
		ArrayList<Unit> result = robb.getSuggestion(center.getGarage());
		
		assertFalse(result.contains(u1)); // u1 not needed = firetruck
		assertTrue(result.contains(u2)); // u2 = needed => Policecar
	}
	
}
